package com.lsc.admin.demo.bean.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lsc.admin.demo.api.UserService;
import com.lsc.admin.demo.bean.ITestBean;
import com.lsc.admin.demo.entity.User;
import com.smf.lsc.core.util.SpringBootBeanUtil;

public class MyTestBeanImpl implements ITestBean {

	@Autowired
	private UserService userService;
	
	@Override
	public void getTest() {
		// TODO Auto-generated method stub
		/*
		 *  反射调用
		 *  适用：动态调用某些类的某些方法。
		 */
		try {
            //从ApplicationContext中取出已创建好的的对象
            ApplicationContext applicationContext = SpringBootBeanUtil.getApplicationContext();
            //反射创建serviceimpi实体对象，和实体类
            Class<?> ServiceImplType = Class.forName("com.lsc.admin.demo.service.impl.UserServiceImpl");
//            Class<?> entityType = Class.forName(package+className);
            //反射设置方法参数。
            Method method = ServiceImplType.getDeclaredMethod("list",null);
            List<User> list=(List<User>) method.invoke(applicationContext.getBean(ServiceImplType),null);
            
            System.out.println("my"+list.size());
            } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
		
	}

}
